package com.ruoyi.project.cms.sm.role.controller;

import com.ruoyi.common.constant.CMSConstants;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.cms.sm.role.domain.SMRole;
import com.ruoyi.project.cms.sm.role.service.ISMRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ：MrL
 * 时间：2020-05-19
 */
@Controller
@RequestMapping("/cms/sm/role")
public class SMRoleController extends BaseController {

    // 当前控制类的请求地址前缀
    private String prefix = "cms/sm/role";

    @Autowired
    private ISMRoleService roleService;

    @GetMapping()
    public String role() {
        return prefix + "/role";
    }

    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SMRole role) {
        startPage();
        List<SMRole> list = roleService.selectRoleList(role);
        return getDataTable(list);
    }

    /**
     * 新增角色
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存角色
     */
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated SMRole role) {
        if (CMSConstants.ROLE_NAME_NOT_UNIQUE.equals(roleService.checkRoleNameUnique(role))) {
            return error("新增角色'" + role.getRoleName() + "'失败，角色名称已存在");
        }
        System.out.println(role);
        return toAjax(roleService.insertRole(role));
    }

    /**
     * 校验角色名称
     */
    @PostMapping("/checkRoleNameUnique")
    @ResponseBody
    public String checkRoleNameUnique(SMRole role) {
        return roleService.checkRoleNameUnique(role);
    }

    /**
     * 批量删除角色
     *
     * @param ids
     * @return
     */
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        try {
            return toAjax(roleService.deleteRoleByIds(ids));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    /**
     * 修改角色
     */
    @GetMapping("/edit/{roleId}")
    public String edit(@PathVariable("roleId") Long roleId, ModelMap mmap) {
        mmap.put("role", roleService.selectRoleById(roleId));
        return prefix + "/edit";
    }

    /**
     * 修改保存角色
     */
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated SMRole role) {
        // 先校验该角色是否允许被操作
        roleService.checkRoleAllowed(role);
        if (CMSConstants.ROLE_NAME_NOT_UNIQUE.equals(roleService.checkRoleNameUnique(role))) {
            return error("修改角色'" + role.getRoleName() + "'失败，角色名称已存在");
        }
        return toAjax(roleService.updateRole(role));
    }

    /**
     * 查看角色详情
     *
     * @param roleId
     * @param mmap
     * @return
     */
    @GetMapping("/detail/{roleId}")
    public String detail(@PathVariable("roleId") Long roleId, ModelMap mmap) {
        mmap.put("name", "role");
        mmap.put("role", roleService.selectRoleById(roleId));
        return prefix + "/detail";
    }

}
